package com.zhaohang.cn.mybasicactivity;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhaohang.cn.constant.ConstantUtils;
import com.zhaohang.cn.utils.Util;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

public abstract class BasicActivity extends BasicAllActivity {

    private LinearLayout actionbar;
    private LinearLayout handlinear;
    private LinearLayout contentLayout;
    private RelativeLayout left;
    private TextView titletext;
    private RelativeLayout right;
    private View mView;
    private SmartRefreshLayout mSmartRefreshLayout;
    private LayoutInflater mInflater;
    private View mChild;
    private View mLoadView;
    private View mErrorView;
    private ViewGroup mNoData;
    private int isShow = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_basic);
        actionbar = (LinearLayout) findViewById(R.id.myactionbar);
        handlinear = (LinearLayout) findViewById(R.id.handlinear);
        contentLayout = (LinearLayout) findViewById(R.id.content_layout);
        left = (RelativeLayout) findViewById(R.id.left);
        titletext = (TextView) findViewById(R.id.titletext);
        right = (RelativeLayout) findViewById(R.id.right);

        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, CompontApplayCation.mStatbarHight);
        actionbar.setLayoutParams(params);

        showState(setShowState());
        left.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BasicActivity.this.finish();
            }
        });

        initView();
    }

    private void initView() {
        mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mView = mInflater.inflate(setContentSuccessView(), null);
        LinearLayout.LayoutParams  mLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        contentLayout.addView(mView, mLayoutParams);

        getInitView();

        if (!isConnected()){
            ViewGroup noNet= (ViewGroup)mView.findViewById(noNetView());

                if (noNet==null){
                   return;
                }else {

                    for (int i = 0; i < noNet.getChildCount(); i++) {
                        noNet.getChildAt(i).setVisibility(View.GONE);
                    }

                    if (mChild==null) {
                        LinearLayout.LayoutParams  layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                LinearLayout.LayoutParams.MATCH_PARENT);
                        mChild = mInflater.inflate(R.layout.nonet_layout, null);
                        noNet.addView(mChild,layoutParams);
                    }

                    initNoNetView(mChild, noNet);
                }
        }else {

            showViewWitheData();

        }

    }

    /**
     *
     * @return 在指定的view上展示无网的界面的view
     */
    protected int noNetView(){
        return 0;
    }

    /**
     * 初始化view
     */
    protected abstract void getInitView();

    private void initNoNetView(final View mChild, final ViewGroup noNet) {

        mSmartRefreshLayout = (SmartRefreshLayout) mChild.findViewById(R.id.refreshLayout);
        //下拉刷新
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                //                mSmartRefreshLayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                if (!isConnected()) {
                    mSmartRefreshLayout.finishRefresh();
                } else {
                    for (int i = 0; i < noNet.getChildCount(); i++) {
                        noNet.getChildAt(i).setVisibility(View.VISIBLE);
                    }
                    mChild.setVisibility(View.GONE);
                    mSmartRefreshLayout.finishRefresh();
                    showViewWitheData();
                }
            }
        });
    }

    private void showViewWitheData() {
        mNoData = mView.findViewById(setNoDataView());
        if (mNoData ==null){
            return;
        }else {
            //加载中的布局
            if (mLoadView == null) {
                LinearLayout.LayoutParams  layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                mLoadView = mInflater.inflate(R.layout.page_loading, null);
                mNoData.addView(mLoadView,0,layoutParams);
            }
            if (mErrorView == null) {
                LinearLayout.LayoutParams  layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT);
                mErrorView =  mInflater.inflate(R.layout.page_error, null);
                mNoData.addView(mLoadView,1,layoutParams);
            }

            isShow = 0;

            //根据是否有数据来展示View
            showDataPager();


            changeView();
        }
    }

    //根据数据自动切换UI
    private void changeView() {
        //去网络获取数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                //谁用谁传
                Object obj = getNetData();
                //判断数据,切换状态
                isShow = checkData(obj);
                Util.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {
                        //根据状态切换UI
                        showDataPager();
                    }
                });


            }
        }).start();
    }
    private int checkData(Object obj){
        //如果当前的对象为空,那么肯定是错误界面
        if (obj == null) {
            return 1;
        } else {
            //这里返回就是成功了吗?
            //如果走到这里有两种情况,一个是bean对象,一个集合,
            //如果是集合,那么如果集合里的数据个数为0,那么也是失败的
            if (obj instanceof List) {
                //说明是集合
                List list = (List) obj;
                if (list.size() > 0) {
                    //说明有数据
                    return 2;
                } else {
                    //说明没有数据
                    return 1;
                }
            } else {
                //不是集合,是对象
                return 2;
            }
        }
    }


    protected Object getNetData(){

        return "";
    }

    private void showDataPager() {
        for (int i = 0; i < mNoData.getChildCount(); i++) {
            mNoData.getChildAt(i).setVisibility(View.GONE);
        }
        switch (isShow){
            case 0:
                mLoadView.setVisibility(View.VISIBLE);
                break;
            case 1:
                mErrorView.setVisibility(View.VISIBLE);
                break;
            case 2:
                for (int i = 2; i < mNoData.getChildCount(); i++) {
                    mNoData.getChildAt(i).setVisibility(View.VISIBLE);
                }
                break;
        }

    }

    /**
     *
     * @return 展示是否有数据的view
     */
    protected int setNoDataView() {

        return 0;
    }

    /**
     * @param showStyle 状态一位全背景，状态二为非全背景
     */
    private void showState(int showStyle) {
        switch (showStyle) {
            case ConstantUtils.LINEARSHOW:
                actionbar.setVisibility(View.VISIBLE);
                handlinear.setVisibility(View.VISIBLE);
                break;
            case ConstantUtils.RELEATSHOW:
                actionbar.setVisibility(View.GONE);
                handlinear.setVisibility(View.GONE);
                break;
        }
    }

    /**
     * @return 怎么显示布局，是整体的还是局部的
     */
    protected  int setShowState(){

        return ConstantUtils.LINEARSHOW;
    }

    /**
     *
     * @return 初始化view布局
     */
    protected abstract int setContentSuccessView();


    /**
     * @return 线性布局的头布局
     */
    protected LinearLayout getHandLinear() {
        if (handlinear == null) {
            handlinear = (LinearLayout) findViewById(R.id.handlinear);
        }
        return handlinear;
    }

    /**
     * 获取最顶部导航栏
     */
    protected LinearLayout getActionLinear(){
        if (actionbar==null){
            actionbar = (LinearLayout) findViewById(R.id.myactionbar);
        }
        return actionbar;
    }

    /**
     * 设置左边按钮里是文字 setLeftShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    protected void setLeftShow(int tag, int resID) {
        if (null == left) {
            left = (RelativeLayout) findViewById(R.id.left);
        }
        left.removeAllViews();
        left.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            left.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            left.addView(view, params);
        } else if (tag == 3) {
            View view = getLayoutInflater().inflate(R.layout.weightback, null);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            left.addView(view, params);
        }
    }

    protected void setTitleText(String title) {

        if (titletext==null){
            titletext = (TextView) findViewById(R.id.titletext);
        }
        titletext.setText(title);
    }
    /**
     * 设置右边边按钮里是文字 setRightShow(这里用一句话描述这个方法的作用) (这里描述这个方法适用条件 – 可选)
     *
     * @throws
     * @since 1.0.0
     */
    protected void setRightShow(int tag, int resID) {
        if (null == right) {
            right = (RelativeLayout) findViewById(R.id.right);
        }
        right.removeAllViews();
        right.setVisibility(View.VISIBLE);
        if (tag == 0) {
            View view = getLayoutInflater().inflate(R.layout.textview, null);
            TextView text = view.findViewById(R.id.text);
            text.setText(this.getResources().getString(resID));
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            right.addView(view, params);
        } else if (tag == 1) {
            View view = getLayoutInflater().inflate(R.layout.imageview, null);
            ImageView imageView = view.findViewById(R.id.icon);
            imageView.setImageResource(resID);
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            right.addView(view, params);
        }
    }

}
